<template>
  <el-row type="flex" class="row-bg">
    <el-col :span="4" style="text-align:left!important;">
      <span style="color:#fff;font-size:20px;" class="gohome" @click="homepage">GuGu后台管理系统</span>
    </el-col>
    <el-col :span="20" style="text-align:right!important;">
      <div class="user_block">
        <!-- div class="help" @click="showMessage">
          <div id="msg_box"  v-show="msgShow" :class="msgShow?'twinkle':''">{{noticeNum}}</div>
          <img :src="clock"  class="clock">
        </div -->
        <el-dropdown trigger="click" @command="handleCommand" style="margin-right:41px!important;float:right;">
          <span class="el-dropdown-link" style="color:#fff;">
            {{username}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <!--<el-dropdown-item command="bindPhone">手机绑定</el-dropdown-item>-->
            <!--<el-dropdown-item command="bindEmail">邮箱绑定</el-dropdown-item>-->
            <el-dropdown-item command="cPassword">修改密码</el-dropdown-item>
            <el-dropdown-item command="signOut">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <!-- <div class="msg_block" @click="showMessage">
          <div id="msg_box"  v-show="msgShow" :class="msgShow?'twinkle':''">{{noticeNum}}</div>
          <img :src="clock"  class="clock">
      </div> -->
      <div class="message" v-show="ismessage">
        <header class="messageH">
          <span class="meeeageN">消息提示</span>
          <el-button size="mini" type="primary">全部已读</el-button>
          <el-button size="mini" type="primary">清空</el-button>
        </header>
        <div>
          <div class="messageList">
            <div class="mListLeft">
              <span class="dot"></span>
              <span class="mLeftName">产品名称</span>
              <span class="mLeftProblem">问题描述</span>
            </div>
            <span class="mListRight state">缺陷待审核</span>
            <span class="mListRight stateTime">小鱼儿 状态 2018-07-12</span>
          </div>
        </div>

      </div>
    </el-col>
    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="changePassword" width="600px" class="headerleft" :close-on-click-modal="false">
      <el-form label-width="80px" :model="changePwdForm" :rules="changePwdRules" ref="changePwdForm">
        <el-form-item label="旧密码" prop="old" style="padding-bottom:15px">
          <el-input v-model="changePwdForm.old" placeholder="请输入当前密码" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new" style="padding-bottom:15px">
          <el-input v-model="changePwdForm.new" placeholder="请输入6到20个字符" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newf">
          <el-input v-model="changePwdForm.newf" placeholder="请输入6到20个字符" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div class="setbutton1">
        <el-button @click="clPassword('changePwdForm')">取 消</el-button>
        <el-button type="primary" @click="chanPassword('changePwdForm')">确 定</el-button>
      </div>
    </el-dialog>
  </el-row>

</template>

<script>
import logo from "@/assets/images/logo.png";
import adminService from "../../service/adminService";
import { getUserId, getUserName } from "@/utils/auth";
export default {
  name: "headerBar",
  data() {
    // 自定义验证规则，密码
    let validatePass1 = (rule, value, callback) => {
      // 6-20位, 数字, 字母, 字符至少包含两种, 同时不能包含中文和空格
      let reg = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\s\u4e00-\u9fa5]{6,20}$/;
      if (!reg.test(value)) {
        callback(new Error("密码长度需6-20位，且包含字母和字符"));
      } else if (value == this.changePwdForm.old) {
        callback(new Error("新密码不能与旧密码相同！"));
      } else {
        callback();
      }
    };
    // 验证密码是否重复，确认密码
    let validatePass2 = (rule, value, callback) => {
      if (value !== this.changePwdForm.new) {
        callback(new Error("两次密码输入不一致"));
      } else {
        callback();
      }
    };
    return {
      logo: logo,
      bindPhone: {
        phoneBindStep1: false,
        phoneBindStep2: false,
      },
      bindMail: {
        mailBindStep1: false,
        mailBindStep2: false,
        mailBindStep3: false,
      },
      changePwdForm: {
        old: "",
        new: "",
        newf: "",
      },
      changePwdRules: {
        old: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
        new: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePass1, trigger: "blur" },
        ],
        newf: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" },
        ],
      },
      filterName: "所有项目",
      ismessage: false,
      mobileno: "",
      mailActive: true,
      changePassword: false,
      username: "",
    };
  },
  computed: {},
  created: function () {
    this.username = getUserName();
  },
  mounted: function () {},
  methods: {
    homepage() {},
    /**
     * 取消修改密码
     * @param changePwdForm 修改密码Form表单
     */
    clPassword(changePwdForm) {
      this.changePassword = false;
      this.$refs[changePwdForm].clearValidate();
    },
    /**
     * 修改密码
     * @param changePwdForm 修改密码Form表单
     */
    chanPassword(changePwdForm) {
      let _this = this;
      let dataInfo = {
        userid: getUserId(),
        password: this.changePwdForm.old,
        newpass: this.changePwdForm.newf,
      };

      this.$refs[changePwdForm].validate((valid) => {
        if (valid) {
          adminService.updatePwd(dataInfo).then((res) => {
            var data = res.data;
            if (data.result === 0) {
              _this.$message({
                message: "重置密码成功",
                type: "success",
              });
            } else {
              _this.$message({
                message: data.message,
                type: "error",
              });
            }
            this.changePassword = false;
          });
        } else {
          return false;
        }
      });
    },
    handleCommand(command) {
      if (command === "cPassword") {
        this.changePassword = true;
        this.changePwdForm.old = "";
        this.changePwdForm.new = "";
        this.changePwdForm.newf = "";
      }
      if (command === "signOut") {
        const dataInfo = {
          userid: this.$store.state.user.userid,
        };
        this.$store
          .dispatch("LogOut", JSON.stringify(dataInfo))
          .then((data) => {
            if (data.data.result === 0) {
              this.$message({
                message: "注销成功",
                type: "success",
              });
              this.$router.push("/login");
            } else {
              this.$message({
                message: data.data.message,
                type: "error",
              });
            }
          });
      }
    },
  },
};
</script>

<style scoped>
@keyframes twinkle {
  from {
    background: #13569b;
    color: #fff;
  }
  to {
    background: #fff;
    color: #13569b;
  }
}

.el-row {
  height: 50px;
}

.logo {
  width: 28px;
  height: 33px;
  margin-top: 8px;
  float: left;
  margin-right: 15px;
}

.user {
  width: 28px;
  height: 28px;
  margin-top: 12px;
  margin-right: 11px;
}

.breadcrumb {
  line-height: 50px;
  color: #fff;
}

.breadcrumb span {
  color: #fff !important;
}

.clock {
  width: 22px;
  height: 22px;
  float: right;
  margin-top: 15px;
  margin-right: 3px;
}

#msg_box {
  float: right;
  width: 20px;
  height: 20px;
  background: #13569b;
  color: #fff;
  border-radius: 50%;
  font-size: 10px;
  text-align: center;
  line-height: 20px;
  margin-top: 15px;
}

.twinkle {
  animation: twinkle 0.5s both infinite;
}

.msg_block {
  float: right;
  height: 50px;
  margin-right: 25px;
}

.user_block {
  float: right;
  height: 50px;
}

.headerleft {
  text-align: left;
}

.setbutton {
  margin-top: 70px;
  text-align: right;
}

.setbutton1 {
  text-align: right;
}

.bindphone {
  color: #337bc4;
}

.showphone {
  padding-left: 20px;
}

.unbind {
  float: right;
  padding-right: 60px;
  cursor: pointer;
}

.showimg {
  margin-top: 3px;
}

.emailtle {
  font-size: 14px;
  height: 20px;
  line-height: 20px;
  color: #565656;
  margin-left: 40px;
}

/* 消息提示 */
.message {
  width: 300px;
  height: 380px;
  border: 1px solid #d9d9d9;
  position: relative;
  top: 50px;
  left: 625px;
  z-index: 20;
  background-color: #fff;
  color: #565656;
}

.meeeageN {
  font-size: 12px;
  height: 17px;
  line-height: 17px;
  padding: 11px 80px 11px 10px;
}

.messageH {
  color: #565656;
  height: 40px;
  line-height: 40px;
}

.messageList {
  width: 100%;
  height: 50px;
  padding: 5px 0;
  background-color: #ecf6ff;
  position: relative;
  line-height: 0;
}

.mListLeft {
  float: left;
  font-size: 13px;
}

.mListRight {
  font-size: 12px;
}

.dot {
  width: 6px;
  height: 6px;
  background-image: url("/static/images/Oval 4@2x.png");
  position: relative;
  display: block;
  top: 27px;
  left: 10px;
}

.mLeftName {
  height: 50px;
  line-height: 50px;
  margin: 21px 5px 21px 26px;
}

.state {
  position: absolute;
  right: 10px;
  top: 15px;
  color: #5399e1;
}

.stateTime {
  position: absolute;
  right: 10px;
  top: 45px;
  color: #a0a0a0;
}

.gohome {
  cursor: pointer;
}
</style>
